package com.cn.algorithm02.class04;

/***
 * @author: hels
 * @description: 使用双向链表实现队列
 **/
public class C02_MyQueueWithLink {
     static NodeDouble head;
     static NodeDouble tail;
     static int size;

    public static void main(String[] args) {
        NodeDouble node1 = new NodeDouble(1);
        NodeDouble node2 = new NodeDouble(2);
        NodeDouble node3 = new NodeDouble(3);

        C02_MyQueueWithLink myqueue = new C02_MyQueueWithLink();
//        myqueue.add(node1);
//        myqueue.add(node2);
        System.out.println(myqueue);
        myqueue.delete();
        System.out.println(myqueue);

    }

    public C02_MyQueueWithLink() {
        size = 0;
    }

    // add 放到尾巴上
    public void add(NodeDouble node) {
        if (size == 0) {
            head = node;
            tail = node;
        } else if(size == 1){
            tail.next = node;
            node.last = tail;
            tail = node;
            head.next = tail;
        } else {
            tail.next = node;
            node.last = tail;
            tail = node;
        }
        size++;
    }

    // delete 返回当前head 队列先进先出
    public NodeDouble delete() {
        if (size == 0 || size == 1) {
            head = null;
            tail = null;
            if (size > 0) size--;
            return head;
        } else if (size == 2) {
            head.next = null;
            tail.last = null;
            head = tail;
        } else {
            NodeDouble point = head.next;
            head.next = null;
            head = point;
            head.next = null;
        }
        if (size > 0) size--;
        return head;
    }

    public  int size() {
        return size;
    }

    public  boolean isEmpty() {
        return size() == 0;
    }


    @Override
    public String toString() {
        Integer headvalue = null,tailvalue = null;
        if (head != null) {
            headvalue = head.value;
        }
        if (tail != null) {
            tailvalue = tail.value;
        }
        return "C02_MyQueueWithLink{value: " + headvalue + ", tail: " + tailvalue + ", size: " + size + "}";
    }
}
